package com.foton.lotterydomain.rule.service.engine;

import com.foton.lotterycommon.common.Constants;
import com.foton.lotterydomain.rule.model.aggregates.TreeRuleRich;
import com.foton.lotterydomain.rule.model.req.DecisionMatterReq;
import com.foton.lotterydomain.rule.model.vo.TreeNodeVO;
import com.foton.lotterydomain.rule.model.vo.TreeRootVO;
import com.foton.lotterydomain.rule.service.logic.LogicFilter;

import java.util.Map;

/**
 * @Author RenPu
 * @Date 2024/1/4 17:53
 * @Version 1.0
 * @Description:
 **/
public abstract class BaseEngine extends EngineConfig implements EngineFilter{

    protected TreeNodeVO engineDecisionMaker(TreeRuleRich treeRuleRich, DecisionMatterReq matter){

         //1: 根节点对象
        TreeRootVO treeRoot = treeRuleRich.getTreeRoot();

        //2:根据根节点对象，获取根节点ID值
        Long rootNodeId = treeRoot.getTreeRootNodeId();

        //3:获取所有子节点的集合
        Map<Long, TreeNodeVO> treeNodeMap = treeRuleRich.getTreeNodeMap();

        //4:根据根节点ID值获取，对应的子节点对象
        TreeNodeVO treeNodeVO = treeNodeMap.get(rootNodeId);

        // 节点类型[NodeType]；1子叶、2果实

        //5:校验是否属于子节点，则属于果实节点则直接返回；
        // 属于子节点根据规则key获取对应的规则逻辑handler,进行获取决策值
        //逻辑决策器根据决策值，返回下一个nextNode对象
        while (Constants.NodeType.STEM.equals(treeNodeVO.getNodeType())){
            String ruleKey = treeNodeVO.getRuleKey();
            LogicFilter logicFilter = filterContainer.get(ruleKey);
            String matterValue = logicFilter.matterValue(matter);
            Long nextNode = logicFilter.filter(matterValue, treeNodeVO.getTreeNodeLineInfoList());
            treeNodeVO=treeNodeMap.get(nextNode);
        }

        return treeNodeVO;
    }


}
